<?php

    function connect(){
        $host = "192.168.12.243";
        $username = "root";
        $pwd = "123456";
        $db_name = "crm_system";
        $dsn = "mysql:host=$host;dbname=$db_name";
        try{
            $pdo = new pdo($dsn,$username,$pwd);
            return $pdo;
        }catch (PDOException $e){
            echo $e->getMessage();
        }
    }
    function dclData($sql){
        try{
            $pdo=connect();
            $num = $pdo->exec($sql);
            $pdo = null;
            if($num>0){
                return true;
            }else{
                return false;
            }
        }catch (PDOException $e){
            echo $e->getMessage();
        }
    }
    function getAllData($sql){
        $pdo = connect();
        $res = $pdo->query($sql);
        if($res){
            foreach ($res as $value) {
                $rows[]=$value;
            }
        }
        $pdo = null;
        return isset($rows)?$rows:null;
    }
    function getRowNum($sql){
        $pdo = connect();
        $res = $pdo->query($sql);
        $rowNum = $res->rowCount();
        $pdo = null;
        return $rowNum;
    }
    //$page当前页数
    //listNum  每页显示条数
    function page($page,$sql,$listNum){
        $pdo=connect();


        $pageBegin = ($page-1)*$listNum;
        $sql .= " limit $pageBegin,$listNum";
        $res = $pdo->query($sql);
        if($res->rowCount()>0){
            foreach ($res as $value) {
            $rows[]=$value;
            }
        }
        else{
            $rows = null;
        }
        $pdo = null;
        return $rows;


    }

